package com.hziee.graduate.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hziee.graduate.ticket.dao.CinemaDao;
import com.hziee.graduate.ticket.dao.NoDao;
import com.hziee.graduate.ticket.dao.RegionDao;
import com.hziee.graduate.ticket.dao.UserDao;
import com.hziee.graduate.ticket.dto.R;
import com.hziee.graduate.ticket.entity.Cinema;
import com.hziee.graduate.ticket.entity.Region;
import com.hziee.graduate.ticket.entity.User;
import com.hziee.graduate.ticket.service.CinemaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service("cinemaService")
public class CinemaServiceImpl extends ServiceImpl<CinemaDao, Cinema> implements CinemaService {

    @Resource
    private CinemaDao cinemaDao;
    @Resource
    private NoDao noDao;
    @Resource
    private UserDao userDao;
    @Resource
    private RegionDao regionDao;

    @Override
    @Transactional
    public R<Object> addCinema(Map<String, Object> map) {
        // 影院名称
        String unitName = map.get("unitName").toString();
        // 影院id
        Integer id = Integer.parseInt(map.get("id").toString());
        // 用户Id
        Long userId = Long.parseLong(map.get("userId").toString());
        // 影院地址
        String unitAddress = map.get("unitAddress").toString();
        // 将影院插入到region表中
        Long primaryNo = noDao.getPrimaryNo("t_region");
        Region region = new Region();
        region.setId(Integer.parseInt(primaryNo.toString()));
        region.setName(unitName);
        region.setPid(id);
        region.setSname(unitName);
        region.setMername(unitAddress);
        region.setLevel(4);
        regionDao.insert(region);
        int t_region = noDao.updatePrimaryNo("t_region");
        // 将影院插入到cinema表中
        Cinema cinema = new Cinema();
        cinema.setCinemaName(unitName);
        cinema.setRegionId(id);
        cinema.setCreatePerson(userId);
        cinema.setCinemaAddress(unitAddress);
        cinema.setSelfId(primaryNo);
        cinema.setCreateTime(LocalDateTime.now());
        cinemaDao.insert(cinema);
        return R.success();
    }

    @Override
    public R<List<Cinema>> getCinemaByRegionId(Map<String, String> map) {
        if(map.get("regionId").isEmpty()) return R.success();
        List<Cinema> cinemaByReginId = query().eq("region_id", Integer.parseInt(map.get("regionId"))).list();
        return R.success(cinemaByReginId);
    }

    @Override
    @Transactional
    public R<Object> delCinema(Integer regionId) {
        // 先移除region
        Region regionById = regionDao.selectOne(new QueryWrapper<Region>().eq("id", regionId));
        regionById.setDelFlag(1);
        regionDao.update(regionById,new UpdateWrapper<Region>().eq("id",regionId));
        // 再移除cinema,以及下面的用户
        List<Cinema> cinemaList = cinemaDao.selectList(new QueryWrapper<Cinema>().eq("self_id", regionId));
        cinemaList.forEach(item -> {
            item.setDelFlag(1);
            cinemaDao.update(item,new UpdateWrapper<Cinema>().eq("self_id", regionId));
            List<User> userByUnitId = userDao.selectList(new QueryWrapper<User>().eq("unit_id", item.getCinemaId()).eq("del_flag", 0));
            userByUnitId.forEach(user -> {
                user.setDelFlag(1);
                userDao.update(user,new UpdateWrapper<User>().eq("user_id", user.getUserId()));
            });
        });
        // 最后移除用户

        return R.success();
    }
}

